from PyQt5 import QtWidgets, QtGui, QtCore, sip


def set_font():
    font = QtGui.QFont()
    font.setFamily("微软雅黑")
    font.setPointSize(12)
    return font


'''
文章列表实体
'''


class YqDoc:
    def __init__(self):
        # 知识库信息
        self.doc_info = {}
        # 已选中的
        self.checked_boxs = {}
        # 多选框实体-知识库列表
        self.doc_check_boxs = []
        self.font = set_font()
        self.repo = None
        self.ui = None
        self.page_offset = 0
        self.page_limit = 100
        self.current_num = 0
        self.is_select_all = False

    def get_prev_page(self):
        self.get_page(True)

    def get_next_page(self):
        self.get_page(False)

    # 翻页
    def get_page(self, prev_flag: bool):
        if prev_flag:
            # 前一页
            self.page_offset -= self.page_limit
            if self.page_offset < 0:
                self.page_offset = 0
                self.ui.Button_page_prev.setDisabled(True)
            self.get_doc(self.repo, self.ui, False)
        else:
            # 后一页
            self.page_offset += self.page_limit
            self.get_doc(self.repo, self.ui, False)
            if self.page_offset >= 1:
                self.ui.Button_page_prev.setEnabled(True)

    # 发送请求-获取知识库列表下的所有文章列表
    def get_doc(self, repo, ui, change_repo: bool):
        if change_repo:
            self.page_offset = 0
        self.repo = repo
        self.ui = ui
        self.doc_info = {}
        for repo_id, repo_name in repo.selected_radio.items():
            # ?offset=3&limit=2
            get_doc_url = f"/repos/{repo_id}/docs?offset={self.page_offset}&limit={self.page_limit}"
            repo_info = f"{repo_id}#{repo_name}"
            result = repo.YqRequest.do_get(get_doc_url)
            doc_info = {}
            for doc in result.get("data"):
                doc_title = doc["title"]
                doc_slug = doc["slug"]
                doc_info[doc_slug] = doc_title
            self.current_num = len(doc_info.items())
            # 每次请求回来doc列表后，设置下一页按钮是否可操作
            if self.current_num < self.page_limit:
                self.ui.Button_page_next.setDisabled(True)
            else:
                self.ui.Button_page_next.setEnabled(True)
            self.doc_info[repo_info] = doc_info
        self.set_doc(ui)

    # 设置文章列表到多选框
    def set_doc(self, ui):
        for i in range(len(self.doc_check_boxs)):
            sip.delete(self.doc_check_boxs[i])
        self.doc_check_boxs = []
        x, y = 0, 0
        for repo_info, doc_dict in self.doc_info.items():
            for doc_slug, doc_title in doc_dict.items():
                checkBox = QtWidgets.QCheckBox(ui.layoutWidget1)
                checkBox.setObjectName(doc_slug)
                checkBox.setText(doc_title)
                checkBox.setFont(self.font)
                checkBox.setProperty("repo_info", repo_info)
                self.doc_check_boxs.append(checkBox)
                if x == 20:
                    x = 0
                ui.gridLayout_doc.addWidget(checkBox, x + 2, y / 20 + 1, 1, 1)
                x += 1
                y += 1
                checkBox.stateChanged.connect(lambda: self.checkboxState(self.doc_check_boxs))

    def checkboxState(self, cbw):
        self.checked_boxs = {}
        for i in range(len(cbw)):
            if cbw[i].isChecked():
                doc_key = f"{cbw[i].property('repo_info')}#{cbw[i].objectName()}"
                self.checked_boxs[doc_key] = cbw[i].text()

    # 全选与取消全选
    def select_all_doc(self):
        self.is_select_all = not self.is_select_all
        for i in range(len(self.doc_check_boxs)):
            self.doc_check_boxs[i].setChecked(self.is_select_all)
            doc_key = f"{self.doc_check_boxs[i].property('repo_info')}#{self.doc_check_boxs[i].objectName()}"
            self.checked_boxs[doc_key] = self.doc_check_boxs[i].text()
        if self.is_select_all:
            self.ui.Button_select_all.setText("取消全选")
        else:
            self.ui.Button_select_all.setText("全选")
